package com.tengju.bff.interfaces.manage.crm.crowd;

import com.tengju.bff.interfaces.manage.crm.crowd.facade.CrowdAnalysisFacade;
import com.tengju.bff.interfaces.manage.crm.crowd.vo.CrowdListForm;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.export.CrowdAnalysisExportQuery;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.shared.IdObject;
import com.tengju.export.application.ExportService;
import com.tengju.export.domain.record.ExportDataType;
import com.tengju.export.domain.record.ExportFileType;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/5/6
 */
@Validated
@RestController
@Api(tags = "人群分析")
@RequestMapping(value = "manage/crm/crowd")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class CrowdAnalysisController {

    private final CrowdAnalysisFacade crowdAnalysisFacade;
    private final ExportService exportService;

    @ApiOperation(value = "人群分析单选框", notes = "人群分析单选框")
    @RequestMapping(value = "tagList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> tagList() {
        return ApiResponse.newSuccess(crowdAnalysisFacade.tagList());
    }

    @ApiOperation(value = "人群分析table", notes = "人群分析table")
    @RequestMapping(value = "list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> list(@Valid @NotNull @RequestBody CrowdListForm form) {
        List<com.tengju.data.domain.model.user.UserIdCode> dataGenSecIdCodes = ServletContextUtil.getServletContext().getDataGenSecIdCodes();
        form.setGenSecIdCodes(dataGenSecIdCodes.stream().map(IdObject::getValue).map(UserIdCode::new).collect(Collectors.toList()));
        return ApiResponse.newSuccess(crowdAnalysisFacade.list(form));
    }

    @ApiOperation(value = "下载人群分析table", notes = "下载人群分析table")
    @RequestMapping(value = "download", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<Object> asyncExport(@Valid @NotNull @RequestBody CrowdListForm form) {
        if (form.getBelongGenSecIdCode() == null) {
            throw new BizException("必须选择所属所属sec");
        }
        long staffId = ServletContextUtil.getServletContext().getStaffId().getValue();
        String realName = ServletContextUtil.getServletContext().getRealName();
        List<UserIdCode> genSecIdCodes = ServletContextUtil.getServletContext().getGenSecIdCodes();
        CrowdAnalysisExportQuery exportQuery = new CrowdAnalysisExportQuery();
        exportQuery.setGenSecIdCodes(genSecIdCodes);
        exportQuery.setPageIndex(1);
        exportQuery.setPageSize(50000);
        exportQuery.setIdCode(form.getIdCode());
        exportQuery.setBelongGenSecIdCode(form.getBelongGenSecIdCode());
        exportQuery.setBelongEcIdCode(form.getBelongEcIdCode());
        exportQuery.setBelongServiceProviderIdCode(form.getBelongServiceProviderIdCode());
        exportQuery.setDirect(form.isDirect());
        exportQuery.setTagName(form.getTagName());
        exportQuery.setTagValue(form.getTagValue());
        exportQuery.setOrderColumn(form.getOrderColumn());
        exportQuery.setOrderType(form.getOrderType());
        exportService.exportData("人群分析列表", ExportFileType.XLSX,
                "crowdAnalysisExportDataSourceRepository",
                ExportDataType.TABLE, exportQuery,
                staffId, realName);
        return ApiResponse.newSuccess();
    }
}
